home *** CD-ROM | disk | FTP | other *** search
/ United Public Domain Gold 2 / United Public Domain Gold 2.iso / utilities / pu250.dms / pu250.adf / Graphics / Sprites / Example1.c < prev    next >
C/C++ Source or Header  |  1992-04-28  |  10KB  |  304 lines

  1. /***********************************************************/
  2. /*                                                         */
  3. /* Amiga C Encyclopedia (ACE) V3.0      Amiga C Club (ACC) */
  4. /* -------------------------------      ------------------ */
  5. /*                                                         */
  6. /* Book:    ACM Graphics                Amiga C Club       */
  7. /* Chapter: Sprites                     Tulevagen 22       */
  8. /* File:    Example1.c                  181 41  LIDINGO    */
  9. /* Author:  Anders Bjerin               SWEDEN             */
  10. /* Date:    92-04-28                                       */
  11. /* Version: 1.00                                           */
  12. /*                                                         */
  13. /*   Copyright 1992, Anders Bjerin - Amiga C Club (ACC)    */
  14. /*                                                         */
  15. /* Registered members may use this program freely in their */
  16. /*     own commercial/noncommercial programs/articles.     */
  17. /*                                                         */
  18. /***********************************************************/
  19.  
  20. /* This program shows how to declare and initialize some sprite data   */
  21. /* and a SimpleSprite structure. It also shows how to reserve a sprite */
  22. /* (sprite 2), and how to move it around. The user moves the sprite by */
  23. /* pressing the arrow keys.                                            */
  24.  
  25.  
  26.  
  27. #include <intuition/intuition.h>
  28. /* Include this file since you are using sprites: */
  29. #include <graphics/sprite.h>
  30.  
  31.  
  32.  
  33. /* Declare the functions we are going to use: */
  34. void main();
  35. void free_memory();
  36.  
  37.  
  38.  
  39. struct IntuitionBase *IntuitionBase = NULL;
  40. /* We need to open the Graphics library since we are using sprites: */
  41. struct GfxBase *GfxBase = NULL;
  42.  
  43.  
  44.  
  45. /* Declare a pointer to a Window structure: */ 
  46. struct Window *my_window = NULL;
  47.  
  48. /* Declare and initialize your NewWindow structure: */
  49. struct NewWindow my_new_window=
  50. {
  51.   40,            /* LeftEdge    x position of the window. */
  52.   20,            /* TopEdge     y positio of the window. */
  53.   200,           /* Width       200 pixels wide. */
  54.   100,           /* Height      100 lines high. */
  55.   0,             /* DetailPen   Text should be drawn with colour reg. 0 */
  56.   1,             /* BlockPen    Blocks should be drawn with colour reg. 1 */
  57.   CLOSEWINDOW|   /* IDCMPFlags  The window will give us a message if the */
  58.   RAWKEY,        /*             user has selected the Close window gad, */
  59.                  /*             or if the user has pressed a key. */
  60.   SMART_REFRESH| /* Flags       Intuition should refresh the window. */
  61.   WINDOWCLOSE|   /*             Close Gadget. */
  62.   WINDOWDRAG|    /*             Drag gadget. */
  63.   WINDOWDEPTH|   /*             Depth arrange Gadgets. */
  64.   WINDOWSIZING|  /*             Sizing Gadget. */
  65.   ACTIVATE,      /*             The window should be Active when opened. */
  66.   NULL,          /* FirstGadget No Custom gadgets. */
  67.   NULL,          /* CheckMark   Use Intuition's default CheckMark. */
  68.   "SPRITES",     /* Title       Title of the window. */
  69.   NULL,          /* Screen      Connected to the Workbench Screen. */
  70.   NULL,          /* BitMap      No Custom BitMap. */
  71.   80,            /* MinWidth    We will not allow the window to become */
  72.   30,            /* MinHeight   smaller than 80 x 30, and not bigger */
  73.   300,           /* MaxWidth    than 300 x 200. */
  74.   200,           /* MaxHeight */
  75.   WBENCHSCREEN   /* Type        Connected to the Workbench Screen. */
  76. };
  77.  
  78.  
  79.  
  80. /*********************************************************************/
  81. /* Extra information:                                                */
  82. /* When we declare the window pointer, the intuition library pointer */
  83. /* etc, we initialize them to point to NULL:                         */
  84. /* struct Window *my_window = NULL;                                  */
  85. /* Since we then know that all of the pointers will point to NULL    */
  86. /* when we start, we can check if they still point to NULL when we   */
  87. /* quit. If they do not point to NULL anymore, we close that window, */
  88. /* library etc.                                                      */
  89. /*********************************************************************/
  90.  
  91.  
  92.  
  93. /********************************************************/
  94. /* 1. Declare and initialize some sprite graphics data: */
  95. /********************************************************/
  96. UWORD chip my_sprite_data[36]=
  97. {
  98.   0x0000, 0x0000,
  99.  
  100.   0x0180, 0x0000,
  101.   0x03C0, 0x0000,
  102.   0x07E0, 0x0000,
  103.   0x0FF0, 0x0000,
  104.   0x1FF8, 0x0000,
  105.   0x3FFC, 0x0000,
  106.   0x7FFE, 0x0000,
  107.   0x0000, 0xFFFF,
  108.   0x0000, 0xFFFF,
  109.   0x7FFE, 0x7FFE,
  110.   0x3FFC, 0x3FFC,
  111.   0x1FF8, 0x1FF8,
  112.   0x0FF0, 0x0FF0,
  113.   0x07E0, 0x07E0,
  114.   0x03C0, 0x03C0,
  115.   0x0180, 0x0180,
  116.  
  117.   0x0000, 0x0000
  118. };
  119.  
  120.  
  121.  
  122. /*******************************************************/
  123. /* 2. Declare and initialize a SimpleSprite structure: */
  124. /*******************************************************/
  125. struct SimpleSprite my_sprite=
  126. {
  127.   my_sprite_data, /* posctldata, pointer to the sprite data. */
  128.   16,             /* height, 16 lines tall. */
  129.   40, 60,         /* x, y, position on the screen. */
  130.   -1,             /* num, this field is automatically initialized when  */
  131.                   /* you call the GetSprite() function, so we set it to */
  132.                   /* -1 for the moment.                                 */
  133. };
  134.  
  135.  
  136.  
  137. void main()
  138. {
  139.   /* Sprite position: */
  140.   WORD x = my_sprite.x;
  141.   WORD y = my_sprite.y;
  142.  
  143.   /* Direction of the sprite: */
  144.   WORD x_direction = 0;
  145.   WORD y_direction = 0;
  146.  
  147.   /* Boolean variable used for the while loop: */
  148.   BOOL close_me = FALSE;
  149.  
  150.   ULONG class; /* IDCMP */
  151.   USHORT code; /* Code */
  152.  
  153.   /* Declare a pointer to an IntuiMessage structure: */
  154.   struct IntuiMessage *my_message;
  155.  
  156.  
  157.  
  158.   /* Open the Intuition Library: */
  159.   IntuitionBase = (struct IntuitionBase *)
  160.     OpenLibrary( "intuition.library", 0 );
  161.   
  162.   if( IntuitionBase == NULL )
  163.     free_memory(); /* Could NOT open the Intuition Library! */
  164.  
  165.  
  166.  
  167.   /* Since we are using sprites we need to open the Graphics Library: */
  168.   /* Open the Graphics Library: */
  169.   GfxBase = (struct GfxBase *)
  170.     OpenLibrary( "graphics.library", 0);
  171.  
  172.   if( GfxBase == NULL )
  173.     free_memory(); /* Could NOT open the Graphics Library! */
  174.  
  175.  
  176.  
  177.   /* We will now try to open the window: */
  178.   my_window = (struct Window *) OpenWindow( &my_new_window );
  179.   
  180.   /* Have we opened the window succesfully? */
  181.   if(my_window == NULL)
  182.     free_memory(); /* Could NOT open the Window! */
  183.  
  184.  
  185.  
  186.   /* Change the colour register 21 - 23: */
  187.   SetRGB4( &my_window->WScreen->ViewPort, 21, 0xF, 0x0, 0x0 ); /* Red */
  188.   SetRGB4( &my_window->WScreen->ViewPort, 22, 0xF, 0xF, 0x0 ); /* Yellow */
  189.   SetRGB4( &my_window->WScreen->ViewPort, 23, 0x0, 0xF, 0x0 ); /* Green */
  190.  
  191.  
  192.  
  193.   /*******************************/
  194.   /* 3. Try to reserve sprite 2: */
  195.   /*******************************/
  196.   if( GetSprite( &my_sprite, 2 ) != 2 )
  197.     free_memory(); /* Could not reserve sprite number 2. */
  198.  
  199.  
  200.  
  201.   /* Stay in the while loop until the user has selected the Close window */
  202.   /* gadget: */
  203.   while( close_me == FALSE )
  204.   {
  205.     /* Stay in the while loop as long as we can collect messages */
  206.     /* sucessfully: */
  207.     while(my_message = (struct IntuiMessage *) GetMsg(my_window->UserPort))
  208.     {
  209.       /* After we have collected the message we can read it, and save any */
  210.       /* important values which we maybe want to check later: */
  211.       class = my_message->Class;
  212.       code  = my_message->Code;
  213.  
  214.  
  215.       /* After we have read it we reply as fast as possible: */
  216.       /* REMEMBER! Do never try to read a message after you have replied! */
  217.       /* Some other process has maybe changed it. */
  218.       ReplyMsg( my_message );
  219.  
  220.  
  221.       /* Check which IDCMP flag was sent: */
  222.       switch( class )
  223.       {
  224.         case CLOSEWINDOW:     /* Quit! */
  225.                close_me=TRUE;
  226.                break;  
  227.  
  228.         case RAWKEY:          /* A key was pressed! */
  229.                /* Check which key was pressed: */
  230.                switch( code )
  231.                {
  232.                  /* Up Arrow: */
  233.                  case 0x4C:      y_direction = -1; break; /* Pressed */
  234.                  case 0x4C+0x80: y_direction = 0;  break; /* Released */
  235.  
  236.                  /* Down Arrow: */
  237.                  case 0x4D:      y_direction = 1; break; /* Pressed */
  238.                  case 0x4D+0x80: y_direction = 0; break; /* Released */
  239.  
  240.                  /* Right Arrow: */
  241.                  case 0x4E:      x_direction = 1; break; /* Pressed */
  242.                  case 0x4E+0x80: x_direction = 0; break; /* Released */
  243.  
  244.                  /* Left Arrow: */
  245.                  case 0x4F:      x_direction = -1; break; /* Pressed */
  246.                  case 0x4F+0x80: x_direction = 0;  break; /* Released */
  247.                }
  248.                break;  
  249.       }
  250.     }
  251.  
  252.  
  253.  
  254.     /* Change the x/y position: */
  255.     x += x_direction;
  256.     y += y_direction;
  257.  
  258.     /* Check that the sprite does not move outside the screen: */
  259.     if(x > 320)
  260.       x = 320;
  261.     if(x < 0)
  262.       x = 0;
  263.     if(y > 200)
  264.       y = 200;
  265.     if(y < 0)
  266.       y = 0;
  267.  
  268.     /* Move the sprite: */
  269.     MoveSprite( 0, &my_sprite, x, y );
  270.  
  271.  
  272.  
  273.     /* Wait for the videobeam to reach the top of the display: (This */
  274.     /* will slow down the animation so the user can see the sprite)  */
  275.     WaitTOF();
  276.   }
  277.  
  278.  
  279.  
  280.   /* Free all allocated memory: (Close the window, libraries etc) */
  281.   free_memory();
  282.  
  283.   /* THE END */
  284. }
  285.  
  286.  
  287.  
  288. /* This function frees all allocated memory. */
  289. void free_memory()
  290. {
  291.   if( my_sprite.num != -1 )
  292.     FreeSprite( my_sprite.num );
  293.  
  294.   if( my_window )
  295.     CloseWindow( my_window );
  296.   
  297.   if( GfxBase )
  298.     CloseLibrary( GfxBase );
  299.  
  300.   if( IntuitionBase )
  301.     CloseLibrary( IntuitionBase );
  302.  
  303.   exit();
  304. }